package com.wanmait.schedule.controller;

import com.alibaba.fastjson.JSONObject;
import com.wanmait.schedule.util.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class CommonControllerAdvice {
    //前后端分离下统一的异常处理
    @ExceptionHandler(Exception.class)
    public Result processException(Exception e, HttpServletRequest request){
        //System.out.println(e.getMessage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("exception",e);
        jsonObject.put("url",request.getRequestURL());
        return Result.error(e.getMessage(),jsonObject);
    }
    @ExceptionHandler(RuntimeException.class)
    public Result errorToken(Exception e, HttpServletRequest request){
        //System.out.println(e.getMessage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("exception",e);
        jsonObject.put("url",request.getRequestURL());
        return Result.error(e.getMessage(),jsonObject);
    }

}
